//package com.roc.template.shiro;
//
//import com.roc.template.shiro.jwt.JwtFilter;
//import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
//import org.apache.shiro.mgt.DefaultSubjectDAO;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.servlet.Filter;
//import java.util.HashMap;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
//@Configuration
//public class ShiroConfig {
//
//    private static final Logger logger = LoggerFactory.getLogger(ShiroConfig.class);
//
//    @Bean
//    public ShiroRealm myShiroRealm(){
//        ShiroRealm myShiroRealm= new ShiroRealm();
//        return myShiroRealm;
//    }
//    @Bean
//    public DefaultWebSecurityManager securityManager(){
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(myShiroRealm());
//        /*
//         * 关闭shiro自带的session，详情见文档
//         * http://shiro.apache.org/session-management.html#SessionManagement-StatelessApplications%28Sessionless%29
//         */
//        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
//        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
//        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
//        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
//
//        securityManager.setSubjectDAO(subjectDAO);
//        return securityManager;
//    }
//
//    //如果没有此name,将会找不到shiroFilter的Bean
//    @Bean(name = "shiroFilter")
//    public ShiroFilterFactoryBean shiroFilter(org.apache.shiro.mgt.SecurityManager securityManager){
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        // 添加自己的过滤器并且取名为jwt
//        Map<String, Filter> filterMap = new HashMap<>();
//        filterMap.put("jwt", jwtFilter());
//        shiroFilterFactoryBean.setFilters(filterMap);
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//
//        /**
//         * 自定义url规则
//         */
//        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();//拦截器, 配置不会被拦截的链接 顺序判断
//        //filterChainDefinitionMap.put("/login","anon");    //所有匿名用户均可访问到Controller层的该方法下
//        filterChainDefinitionMap.put("/login","anon");
//        filterChainDefinitionMap.put("/image/**","anon");
//        filterChainDefinitionMap.put("/css/**", "anon");
//        filterChainDefinitionMap.put("/fonts/**","anon");
//        filterChainDefinitionMap.put("/js/**","anon");
//        filterChainDefinitionMap.put("/swagger-ui.html**", "anon");
//        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
//        filterChainDefinitionMap.put("/v2/api-docs", "anon");
//        filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");
//        filterChainDefinitionMap.put("/**", "authc");    //authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问
//        //filterChainDefinitionMap.put("/**", "user");   //user表示配置记住我或认证通过可以访问的地址
//
//        // 过滤链定义，从上向下顺序执行，一般将放在最为下边
//        filterChainDefinitionMap.put("/**", "jwt");
//
//        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
//        return shiroFilterFactoryBean;
//    }
//
//    @Bean
//    public JwtFilter jwtFilter() {
//        return new JwtFilter();
//    }
//
//    /**
//     * SpringShiroFilter首先注册到spring容器
//     * 然后被包装成FilterRegistrationBean
//     * 最后通过FilterRegistrationBean注册到servlet容器
//     * @return
//     */
//    @Bean
//    public FilterRegistrationBean delegatingFilterProxy(JwtFilter jwtFilter){
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(jwtFilter);
//        filterRegistrationBean.setEnabled(false);
//        return filterRegistrationBean;
//    }
//
//}
